package com.myapp.web.struts2.components;

import java.io.Writer;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsException;
import org.apache.struts2.components.ClosingUIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

@StrutsTag(name="container", tldTagClass="com.etrade.web.jsp.ui.ContainerTag", description="the container of the template")   
public class ContainerUIBean extends ClosingUIBean { 
	
    public ContainerUIBean(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}

	private static final Logger log = LoggerFactory.getLogger(ContainerUIBean.class);   
    
    private String prefixClass;
    
    public static final String SIZE = "struts2_customizize_$size";
    
    public static final String CUST_PARAMETERS = "CUST_PARAMETERS";
    
    private Integer pieceSize;  
    
    public static String KEY_PREFIX = "struts2_customizize_key";
    
    protected static String SEPERATOR = ",";

	public void setPieceSize(Integer pieceSize) {
		this.pieceSize = pieceSize;
	}
	
	private void initializeContentSize(){
		stack.getContext().put(SIZE, 0);
	}
	
	public boolean start(Writer writer) {
        boolean result = super.start(writer);
        evaluateParams();
        try {
			mergeTemplate(writer, buildTemplateName(getDefaultOpenTemplate(), getDefaultOpenTemplate()));
	        initializeContentSize();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return result;
    }

	public void removeContextValue(){
		Map<String,Object> context = stack.getContext();
		Integer s = (Integer)context.get(SIZE);		
		Integer size = s > pieceSize ? pieceSize  : s ;
		for(int i = 0 ; i< size ; i++ ){
			context.remove(KEY_PREFIX+ i);			
		}
		context.remove(SIZE);
	}
	
	public boolean end(Writer writer, String body) {
        //super.evaluateParams();        
        //CharArrayWriter w = new CharArrayWriter();
        try {     
            super.end(writer, body, false);
            mergeTemplate(writer, buildTemplateName(getDefaultTemplate(), getDefaultTemplate()));
            removeContextValue();
        } catch (Exception e) {
        	e.printStackTrace();
            throw new StrutsException(e);
        }
        finally {
            popComponentStack();
        }
        return false;
    }
	
	
	@Override
	protected void evaluateExtraParams() {
		super.evaluateExtraParams();
		if(prefixClass != null && !"".equals(prefixClass)){
			addParameter("prefixClass", prefixClass);
		}
	}

	protected String getDefaultTemplate() {
		return template.concat("-close");
	}
	
	public String getDefaultOpenTemplate() {
		return template;
    }

	@StrutsTagAttribute(description="set the prefix of class property of the sytle")
	public void setPrefixClass(String prefixClass) {
		this.prefixClass = prefixClass;
	}
}
